package com.example.jsonhelper.framework.widget

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.example.jsonhelper.R

/**
 *
 * @author Yingyong Lao
 * 创建时间 2022/8/21 22:35
 * @version 1.0
 */
class LyyDialog(context: Context) {
    private var dialogView: View
    private var dialog:Dialog
    private lateinit var negativeBtn:Button
    private lateinit var positiveBtn:Button
    private lateinit var titleTv:TextView
    private lateinit var contentTv:TextView
    private lateinit var onBtnsClickListener: OnBtnsClickListener
    init {
        dialogView=LayoutInflater.from(context).inflate(R.layout.lyy_dialog,null)
        dialog= Dialog(context,R.style.LYY_DIALOG)
        dialog.setContentView(dialogView)
        dialog.setCancelable(false)
        initViews()
        initEvents()
    }

    private fun initViews() {
        negativeBtn=dialogView.findViewById(R.id.negativeBtn)
        positiveBtn=dialogView.findViewById(R.id.positiveBtn)
        titleTv=dialogView.findViewById(R.id.titleTv)
        contentTv=dialogView.findViewById(R.id.contentTv)
    }

    private fun initEvents() {
        negativeBtn.setOnClickListener {
            onBtnsClickListener.onNegativeBtnClick(this)
        }
        positiveBtn.setOnClickListener {
            onBtnsClickListener.onPositiveBtnClick(this)
        }
    }

    fun show(){
        if (!dialog.isShowing){
            dialog.show()
        }
    }

    fun dismiss(){
        if (dialog.isShowing){
            dialog.dismiss()
        }
    }

    fun setContent(content:String){
        contentTv.setText(content)
    }

    fun setTitle(title:String){
        titleTv.setText(title)
    }
    fun setBtnsClickListener(onBtnClickListener: OnBtnsClickListener){
        this.onBtnsClickListener=onBtnClickListener
    }

    interface OnBtnsClickListener{
        fun onNegativeBtnClick(dialog: LyyDialog)
        fun onPositiveBtnClick(dialog: LyyDialog)
    }
}